﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace WAF.EntityFramework
{
    /// <summary>
    /// 实体类自定义属性
    /// </summary>
    [System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
    public class EntityTableAttribute : Attribute
    {
        public EntityTableAttribute(string label = "", bool log = false)
        {
            Label = label;
            Log = log;
        }

        /// <summary>
        /// 表的描述标签
        /// </summary>
        public string Label { get; private set; }

        /// <summary>
        /// 是否对该表的操作记录日志
        /// </summary>
        public bool Log { get; private set; }
    }

    /// <summary>
    /// 实体类字段自定义属性
    /// </summary>
    [System.AttributeUsage(System.AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public class EntityFieldAttribute : Attribute
    {
        public EntityFieldAttribute(int ordinal, bool isPrimaryKey = false, bool isForeignKey = false, bool isNullable = true, bool isUnique = false, bool isDisplay = false, DbType dbType = DbType.String, string label = "", string refTable = "", string refLableField = "")
        {
            Ordinal         = ordinal;          //字段在类中的顺序
            IsPrimaryKey    = isPrimaryKey;     //是否为主键
            IsForeignKey    = isForeignKey;     //是否为外键
            IsNullable      = isNullable;       //是否可为空
            IsUnique        = isUnique;         //是否是唯一
            IsDisplay       = isDisplay;        //是否在Grid中显示该字段
            FieldDbType     = dbType;           //字段在数据库中的数据类型
            Label           = label;            //用于显示的字段标签
            RefTable        = refTable;         //关联表的名称
            RefLableField   = refLableField;    //关联表的标签字段
        }

        /// <summary>
        /// 字段在类中的顺序（主键id的顺序号为0，其他字段依次递增）
        /// </summary>
        public int Ordinal { get; private set; }

        /// <summary>
        /// 是否为主键
        /// </summary>
        public bool IsPrimaryKey { get; private set; }

        /// <summary>
        /// 是否为外键
        /// </summary>
        public bool IsForeignKey { get; private set; }

        /// <summary>
        /// 是否可为空
        /// </summary>
        public bool IsNullable { get; private set; }

        /// <summary>
        /// 是否唯一
        /// </summary>
        public bool IsUnique { get; private set; }

        /// <summary>
        /// 是否在Grid中显示该字段
        /// </summary>
        public bool IsDisplay { get; private set; }

        /// <summary>
        /// 字段在数据库中的数据类型
        /// </summary>
        public DbType FieldDbType { get; private set; }

        /// <summary>
        /// 用于显示的字段标签
        /// </summary>
        public string Label { get; private set; }
        
        /// <summary>
        /// 关联表的名称
        /// </summary>
        public string RefTable { get; private set; }

        /// <summary>
        /// 关联表的标签字段
        /// </summary>
        public string RefLableField { get; private set; }
    }

    /// <summary>
    /// 实体类字段约束属性
    /// </summary>
    [System.AttributeUsage(System.AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public class EntityFieldConstraintAttribute : Attribute
    {
        public EntityFieldConstraintAttribute(string constraint = "", int minimum = 0, int maximum = 0)
        {
            Constraint  = constraint;   //验证字段类型（"Regular"-字符串正则表达式验证，"Length"-字符串长度验证, "Value"-值验证）
            Minimum     = minimum;      //最小值
            Maximum     = maximum;      //最大值
        }

        /// <summary>
        /// 验证字段类型（"Regular"-字符串正则表达式验证，"Length"-字符串长度验证, "Value"-值验证）
        /// </summary>
        public string Constraint { get; private set; }

        /// <summary>
        /// 最小值
        /// </summary>
        public int Minimum { get; private set; }

        /// <summary>
        /// 最大值
        /// </summary>
        public int Maximum { get; private set; }
    }
}
